/**
 * Created by shulw on 2016/12/21.
 */

angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'limitToFilter', 'filterFilter', 'dateFilter', 'myConstant', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                               CacheService, limitToFilter, filterFilter, dateFilter, myConstant) {


            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            //变量初始化
            //分页数据
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {};


            //缓存数据初始化(需要缓存的key请自定义)
            /*数据格式{
             key1 :value1,
             key2:value2
             }*/
            vm.cached = {};

            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };

            (function () {
                $http.post(config.ctx + "/reserve/bank/info/init/bank", {}, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.cached.BANK_INFO = data.object;
                            var arr = [];
                            vm.cached.RESERVE_LIST = [];
                            for (var index in data.object) {
                                var item = data.object[index];
                                var bankInfo = {'accountNo': item.accountNo, 'desc': item.openBankName};
                                arr.push(bankInfo);
                                vm.cached.RESERVE_LIST.push({'accountNo': item.accountNo, 'bankCode': item.remarkDesc});
                            }
                            vm.cached.BANK_INFO_LIST = arr;
                        } else {
                            alert(data.msg);
                        }
                    });
            }());

            vm.queryDetail = queryDetail;

            vm.addItem = addItem;
            vm.updateItem = updateItem;
            vm.deleteItem = deleteItem;
            vm.reviewPass = reviewPass;
            vm.reviewReject = reviewReject;
            vm.reviewBack = reviewBack;

            /*------------------以上配置通用---------------------*/

            /*------------------以上方法名可选择性通用---------------------*/


            function queryDetail() {

                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                var postData = angular.copy(vm.queryBean);
                postData.pageNum = vm.pagination.pageNum;
                postData.pageSize = vm.pagination.pageSize;
                vm.pagination.queryBean = postData;
                var data = angular.copy(vm.pagination);
                data.list = null;
                $http.post(vm.url, data, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination = data.object;
                            //清空选中记录
                            // CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });

            };

            function addItem() {
                OpenService({
                    modalTitle: '新增基础数据调整',
                    modalBody: 'toAdd',
                    url: 'add',
                    item: {},
                    cached: {
                        RESERVE_LIST: vm.cached.RESERVE_LIST
                    }
                }, function (item) {
                    vm.pagination.list.push(item);
                }, 'addAdjustmentController', '');
            }

            function updateItem() {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length != 1) {
                    alert("必须勾选一条记录！");
                    return;
                }
                var item = CheckboxService.getCheckedNew(vm.pagination.list)[0];
                OpenService({
                    modalTitle: '修改报表调整数',
                    modalBody: 'toUpdate',
                    url: 'update',
                    item: item,
                    cached: {}
                }, function (item) {
                    $log.debug(item);
                },'updateAdjustmentController','');
            }

            function deleteItem() {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length != 1) {
                    alert("必须勾选一条记录！");
                    return;
                }
                var item = CheckboxService.getCheckedNew(vm.pagination.list)[0];
                if (confirm("确定要删除吗！")){
                    var remark = prompt("请输入删除备注！", "");
                    if (!remark){
                        alert("删除备注不能为空")
                    }else {
                        item.remarkDesc = item.remarkDesc+"|"+remark;
                        $http.post('delete', item, config.jsonHeader)
                            .then(function (response) {
                                var data = response.data;
                                if (data.success) {
                                    vm.pagination.list.splice(vm.pagination.list.indexOf(item), 1);
                                    alert("操作成功");
                                } else {
                                    alert(data.msg);
                                }
                            }, function (response) {
                                alert(response.statusText);
                                $log.debug("error");
                            });
                    }

                }
            }

            function reviewPass() {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length < 1) {
                    alert("必须勾选一条记录！");
                    return;
                }
                var item = CheckboxService.getCheckedNew(vm.pagination.list);
                $http.post('review/pass', item, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function reviewReject() {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length < 1) {
                    alert("必须勾选一条记录！");
                    return;
                }
                var item = CheckboxService.getCheckedNew(vm.pagination.list);
                $http.post('review/reject', item, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function reviewBack() {
                if (CheckboxService.getCheckedNew(vm.pagination.list).length < 1) {
                    alert("必须勾选一条记录！");
                    return;
                }
                var item = CheckboxService.getCheckedNew(vm.pagination.list);
                $http.post('review/back', item, config.jsonHeader)
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert("操作成功");
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

        }]);


angular.module('myApp').controller('updateAdjustmentController', ['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', 'FileUploader', 'dateFilter', '$log', 'CheckboxService', 'myConstant', 'DateCalculateService', function ($scope, $uibModalInstance, modalItem, $http,
                                                                                                                                           $httpParamSerializerJQLike, FileUploader, dateFilter, $log, CheckboxService, myConstant, DateCalculateService) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {}
        };

        vm = $scope.vm = modalItem;
        vm.constant = myConstant;

        vm.queryBean = {};

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };

        $scope.save = function () {
            if (vm.item.remarkDesc == ""){
                alert("备注信息不能为空！");
            }
            $http.post(config.ctx + '/reserve/report/adjustment/update', vm.item, config.jsonHeader)
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        alert("操作成功");
                        $uibModalInstance.dismiss('cancel');
                    } else {
                        alert(data.msg);
                    }
                }, function (response) {
                    alert(response.statusText);
                    $log.debug("error");
                });
        };
    }]);


angular.module('myApp').controller('addAdjustmentController', ['$scope', '$uibModalInstance', 'modalItem', '$http',
    '$httpParamSerializerJQLike', 'FileUploader', 'dateFilter', '$log', 'CheckboxService', 'myConstant', 'DateCalculateService', function ($scope, $uibModalInstance, modalItem, $http,
                                                                                                                                           $httpParamSerializerJQLike, FileUploader, dateFilter, $log, CheckboxService, myConstant, DateCalculateService) {

        var vm = $scope.vm = {
            modalTitle: 'please change the title',
            modalBody: '#',
            url: '',
            item: {}
        };

        vm = $scope.vm = modalItem;
        vm.constant = myConstant;

        vm.queryBean = {};

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };

        $scope.save = function () {
            if (vm.item.remarkDesc == ""){
                alert("备注信息不能为空！");
            }
            $http.post(config.ctx + '/reserve/report/adjustment/add', vm.item, config.jsonHeader)
                .then(function (response) {
                    var data = response.data;
                    if (data.success) {
                        alert("操作成功");
                        $uibModalInstance.dismiss('cancel');
                    } else {
                        alert(data.msg);
                    }
                }, function (response) {
                    alert(response.statusText);
                    $log.debug("error");
                });
        };
    }]);